package com.apes.fn.crm.ReturnVisit.model;

import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicUpdate
@DynamicInsert
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = VisitTransferHistoryItem.CONST_TABLE_NAME)

public class VisitTransferHistoryItem extends BaseModel {
    public static final String CONST_TABLE_NAME = "fn_visit_transfer_history_item";

    @Id
    @Comment("编码")
    @Column(name = "id", nullable = false, length = 32)
    @ApesGenerator(code = "fn_visit_transfer_history_item", name = "售后回访转移历史记录明细")
    private String id;

    @NotNull
    @Comment("转移历史编码")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "visit_transfer_history_id", referencedColumnName = "id", nullable=false)
    private VisitTransferHistory visitTransferHistory;

    @Comment("回访单号")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "return_visit_id", referencedColumnName = "id", nullable=false)
    private ReturnVisit returnVisit;

    @Comment("门店")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id", nullable = false)
    private Dept dept;

    @Comment("原回访人员")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "pre_visitor_id", referencedColumnName = "id")
    private PartyRole preVisitor;

    @Comment("回访人员")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "visitor_id", referencedColumnName = "id")
    private PartyRole visitor;

    @States(name = "单据状态", values = {
            @State(id = "create", name = "创建"),
            @State(id = "again", name = "继续跟踪"),
            @State(id = "end", name = "终止"),
            @State(id = "finish", name = "完成")})
    @Column(name = "state", length = 16, nullable = false)
    private String state = "create";

    @Transient
    private String stateName;

    public String getStateName() {
        switch (this.state) {
            case "create":
                return "创建";
            case "again":
                return "继续回访";
            case "end":
                return "终止";
            case "finish":
                return "回访完成";
            default:
                return "";
        }
    }

}
